package com.sdk.pay.transfer.xposed;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;

import com.sdk.pay.transfer.provider.ProviderHelper;
import com.sdk.pay.transfer.utils.LogUtil;

import java.text.DecimalFormat;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

/**
 * Created by Administrator on 2017/9/6.
 */

public class ProductProgram implements IXposedHookLoadPackage {
    private static final int MODEL_COUNT = 10;
    private static final DecimalFormat format = new DecimalFormat("0.00");

    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam param) throws Throwable {
        if (param == null || param.packageName == null || !param.packageName.equals("com.tencent.mm")) {
            return;
        }
        XposedHelpers.findAndHookMethod(Application.class,
                "attach", Context.class, new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        ClassLoader cl = ((Context) param.args[0]).getClassLoader();
                        Class<?> hookclass = null;
                        try {
                            hookclass = cl.loadClass("com.tencent.mm.plugin.collect.ui.CollectMainUI");
                        } catch (Exception e) {
                            LogUtil.log("not find com.tencent.mm.plugin.collect.ui.CollectMainUI");
                            return;
                        }
                        LogUtil.log("find com.tencent.mm.plugin.collect.ui.CollectMainUI");
                        XposedHelpers.findAndHookMethod(hookclass, "onActivityResult", int.class, int.class, Intent.class, new XC_MethodHook() {
                            @Override
                            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                                XposedBridge.log("ProductProgram CollectMainUI");
                                Activity launcher = (Activity) param.thisObject;
                                Intent intent = (Intent) param.args[2];
                                if (intent == null) {
                                    return;
                                }
                                double payFee = intent.getDoubleExtra("ftf_fixed_fee", 0.0D);
                                String payDesc = intent.getStringExtra("ftf_fixed_desc");
                                String qrCodeUrl = intent.getStringExtra("ftf_pay_url");
                                LogUtil.log("hook onActivityResult money:" +
                                        payFee + "|beizhu:"
                                        + payDesc + "|qrCodeUrl:" + qrCodeUrl);
                                ProviderHelper.updateCodeUrlWithReason(payDesc,
                                        qrCodeUrl, launcher);
                            }
                        });
                    }
                });
    }
}
